#include "yolo.h"

yolo::yolo(Mat &inputiamge,string &modelpath,string &classpath){
    input = inputiamge;
    ModelPath = modelpath;
    ClassPath = classpath;
}

Mat yolo::run(){
    // 模型加载
    dnn::Net net;
    net = dnn::readNetFromONNX(ModelPath);
    net.setPreferableBackend(dnn::DNN_BACKEND_OPENCV);
    net.setPreferableTarget(dnn::DNN_TARGET_CPU);

    // 类别加载
    std::ifstream inputFile(ClassPath);
    if (inputFile.is_open())
    {
        std::string classLine;
        while (std::getline(inputFile, classLine))
            classes.push_back(classLine);
        inputFile.close();
    }
    
    // 推理
    Mat blob;
    dnn::blobFromImage(input, blob, 1.0/255.0, Size(320,320), Scalar(), true, false);
    net.setInput(blob);
    outputs = net.forward();  
    // outputs.reshape(1,1);
    return outputs;
}